package com.ynwa;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author gzd
 * @date 2019-09-28 14:57
 */
public class HashMapUnsafe {

    private static Map<String, String> map = new HashMap<String, String>(16);

    public static class TestHashMapThread implements Runnable {

        int start;

        public TestHashMapThread(int start) {

            this.start = start;
        }

        @Override
        public void run() {
            for (int i = 0; i < 50; i++) {
//                System.out.println("--put----");
                map.put(Integer.toString(i), String.valueOf(Math.random() * 100));
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {

        Thread[] threads = new Thread[10];

        for (int i = 0; i < threads.length; i++) {

            threads[i] = new Thread(new TestHashMapThread(i));

        }


        for (int i = 0; i < 10; i++) {

            threads[i].start();
        }

        Thread.sleep(2000);

        System.out.println(map.size());
        Set<String> strings = map.keySet();
        Object[] objects = strings.toArray();
        Arrays.sort(objects);

        Arrays.stream(objects).forEach(System.out::println);

        System.out.println("==========");

        strings.stream().forEach(s -> System.out.println(map.get(s)));

    }
}
